<html>
  <head>
    <script type="text/javascript" src="/js/loadJSON.js"></script>
    <script type="text/javascript" src="/js/background_utils_load_options.js"></script>
    <script type="text/javascript" src="/js/background_utils_save_options.js"></script>
    <script type="text/javascript" src="/js/utils.js"></script>
    <script type="text/javascript" src="/js/extension_utils_requests.js"></script>
    <script type="text/javascript">
      var bExtraDebug = true;
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-549122-14']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = 'https://ssl.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
      goManifest = loadJSON('/manifest.json');
      if (bExtraDebug) debugLog('Manifest: ' + repr(goManifest));
      goOptionsStructure = loadJSON('/options_structure.json');
      if (bExtraDebug) debugLog('Options structure: ' + repr(goOptionsStructure));
      goOptions = loadOptions(goOptionsStructure);
      if (bExtraDebug) debugLog('Options: ' + repr(goOptions));
      var goIconNames = loadJSON('/icon_names.json');

      addRequestHandler(
          "loadManifest",
          function loadManifestRequestHandler(uUnused, oSender, fSuccessCallback, fFailureCallback) {
            fSuccessCallback(goManifest);
          }
      );
      addRequestHandler(
          "loadOptionsStructure", 
          function loadOptionsStructureRequestHandler(uUnused, oSender, fSuccessCallback, fFailureCallback) {
            fSuccessCallback(goOptionsStructure);
          }
      );
      addRequestHandler(
          "loadOptions", 
          function loadOptionsRequestHandler(uUnused, oSender, fSuccessCallback, fFailureCallback) {
            goOptions = loadOptions(goOptionsStructure);
            debugLog("Options loaded: " + repr(goOptions));
            fSuccessCallback(goOptions);
          }
      );
      addRequestHandler(
          "saveOptions", 
          function saveOptionsRequestHandler(oOptions, oSender, fSuccessCallback, fFailureCallback) {
            saveOptions(goOptionsStructure, oOptions);
            goOptions = oOptions;
            debugLog("Options saved: " + repr(goOptions));
            fSuccessCallback();
          }
      );
      addRequestHandler(
          "showPageActionIcon", 
          function showPageActionIconRequestHandler(oIconDetails, oSender, fSuccessCallback, fFailureCallback) {
            debugLog("showPageActionIcon: Request to show page action icon " + 
                repr(oIconDetails.name) + " in tab " + repr(oSender.tab.id) + "...");
            if (!(oIconDetails.name in goIconNames)) {
              debugLog("showPageActionIcon: Icon name unknown.");
              chrome.pageAction.hide(oSender.tab.id);
              fFailureCallback("Icon name " + repr(oIconDetails.name) + " unknown.");
            } else {
              var sIconUrl = goIconNames[oIconDetails.name];
              debugLog("Tab " + oSender.tab.id + " page action icon: " + repr(sIconUrl));
              chrome.pageAction.show(oSender.tab.id);
              chrome.pageAction.setIcon({
                path: sIconUrl,
                tabId: oSender.tab.id
              });
              chrome.pageAction.setTitle({
                title: oIconDetails.title,
                tabId: oSender.tab.id
              });
              fSuccessCallback();
            }
          }
      );
      addRequestHandler(
          "removePageActionIcon", 
          function removePageActionIconRequestHandler(uUnused, oSender, fSuccessCallback, fFailureCallback) {
            debugLog("Tab " + oSender.tab.id + " page action icon removed.");
            chrome.pageAction.hide(oSender.tab.id);
          }
      );
      
      chrome.pageAction.onClicked.addListener(function pageActionClickedHandler(tab) {
        debugLog("Tab " + tab.id + " page action icon clicked.");
        tabRequest(tab.id, "pageActionClicked", null,
          function tabRequestPageActionClickedSuccess() {
          },
          function tabRequestPageActionClickedFailed(sErrorMessage) {
            debugLog("Failed: " + sErrorMessage);
          }
        );
      });
    </script>
  </head>
  <body>
  </body>
</html>
